#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys,urllib,re,md5,os
import MySQLdb
import ConfigParser
import htmllib
import formatter
import string
import time
import thread
import re
import urllib2
import socket
import datetime
class getUserPic:
    def __init__(self):
        self.picDir = "/opt/pic/"
        self.beginTime = time.time()
    
    def mkdir(self,ndate):
        dir1 = int(ndate / 2000 / 2000)
        dir2 = int(ndate / 2000)
        if not os.path.exists('%s%s' %(self.picDir,dir1)):
            os.mkdir('%s%s' %(self.picDir,dir1))
        if not os.path.exists('%s%s/%s' % (self.picDir,dir1,dir2)):
            os.mkdir('%s%s/%s' % (self.picDir,dir1,dir2))
        if not os.path.exists('%s%s/%s/%s' % (self.picDir,dir1,dir2,ndate)):
            os.mkdir('%s%s/%s/%s' % (self.picDir,dir1,dir2,ndate))
        dir = str("%s/%s/%s/%s" %(dir1,dir2,ndate,""))    
        return dir
    
    def se(self):
      limitSql = "select * from user_info where status=0 limit 10"
      db = self.dbConn()
      cursor = db.cursor()
      cursor.execute(limitSql)
      info = cursor.fetchall()
      db.close()
      return info
    
    def download(self,ndate,threadNum):
        image = ndate[3]
        dir = self.mkdir(ndate[0])
        newImg = dir+image.split('/')[-1]
        print "下载："+image
        self.upPic(newImg,ndate[0])
        self.downloadThread(threadNum,image,self.picDir+dir)
        #thread.start_new_thread(self.downloadThread,(threadNum,image,self.picDir+dir))
    
    def upPic(self,pic,id):
        sql = "update user_info set pic='"+pic+"' where id="+str(id)
        db = self.dbConn()
        cursor = db.cursor()
        cursor.execute(sql)
        db.close()
    
    def downloadThread(self,threadNum,url,dir):
        try:
            urllib.urlretrieve(url,dir+url.split('/')[-1])
            print threadNum," thread"
        except:
            print "error download"
    
    def mark(self,data):
        db = self.dbConn()
        for row in data:
            sql = "update user_info set status=1 where id=%d" % (row[0])
            cursor = db.cursor()
            cursor.execute(sql)
        db.close()
    
    def run(self):
        while True:
            userList = self.se()
            if len(userList) < 1:
                break
            self.mark(userList)
            threadNum = 1
            for u in userList:
                self.download(u,threadNum)
                threadNum = threadNum+1
                print "当前",time.time() - self.beginTime
    
    #get config file
    def initConfig(self,configFile):
        config = ConfigParser.ConfigParser();
        config.readfp(open(configFile));
        return config;
    
    def matchCode(self,regular,content):
        str = re.compile(regular,re.I|re.S)
        return str.findall(content)
    
    def dbConn(self,dbServer="default"):
        config = self.initConfig("../config/_db.ini")
        user = str(config.get(dbServer,"user"))
        db   = str(config.get(dbServer,"db"))
        passwd   = str(config.get(dbServer,"passwd"))
        host   = str(config.get(dbServer,"host"))
        conn = MySQLdb.connect(user=user, db=db, passwd=passwd, host=host)
        return conn
if __name__ == "__main__":
    p = getUserPic()
    p.run()
